package com.example.cet4try.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;

import java.util.ArrayList;


public class TableAdapter extends RecyclerView.Adapter {

    private Context context;
    private ArrayList<String> allData;
    private ArrayList<String> data = new ArrayList<String>();
    private int columns,itemHeight = -1;
    private OnItemClickListener onItemClickListener = null;
    //private View selectItem = null;
    //private Drawable k_red,k_grey;

    public TableAdapter(Context context,ArrayList<String> data,int columns) {
        this.context = context;
        this.allData = data;
        if (data.size() >= columns*50){
            for (int i = 0;i < columns*50;i++){
                this.data.add(data.get(i));
            }
        }
        else
            this.data = data;

        this.columns = columns;
//        k_red = context.getResources().getDrawable(R.drawable.k_red);
//        k_grey = context.getResources().getDrawable(R.drawable.k_grey);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.table_item, parent, false);
        return new TableViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TextView item = ((TableViewHolder)holder).item;
        item.setText(data.get(position));
        if (position < columns){
            item.setTextColor(0xff5f9ea0);
            item.setEnabled(false);
            item.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            item.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
        }
        else {
            item.setTag(position);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    /*if (selectItem != null){
                        selectItem.setBackground(k_grey);
                    }
                    selectItem = view;
                    view.setBackground(k_red);*/
                    if (onItemClickListener != null)
                        onItemClickListener.OnItemClick(view,(int)view.getTag());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public int getLineCount(){
        return data.size()/columns;
    }

    public int getItemHeight() {
        if (itemHeight == -1)
            itemHeight = Tools.dp2px(context,20);

        return itemHeight;
    }

    public String[] getItemData(int position){
        return new String[]{data.get(position%columns),data.get(position)};
    }

    public void setItemData(int position,String text){
        data.set(position,text);
    }

    public void loadMore() {
        int c = columns*50+data.size();
        if (c >= allData.size())
            c = allData.size();
        //int s = data.size(),t = 0;
        for (int i=data.size();i<c;i++){
            //t++;
            data.add(allData.get(i));
        }
        /*if (t != 0)
            notifyItemRangeInserted(s,t);*/
        //notifyDataSetChanged();
    }

    private class TableViewHolder extends RecyclerView.ViewHolder{
        public TextView item;
        TableViewHolder(View itemView) {
            super(itemView);
            this.item = (TextView) itemView;
        }
    }

    public interface OnItemClickListener{
        void OnItemClick(View view,int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
